import path from 'path'
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('../views/Home/index.vue'),
  },
  {
    path: '/auth',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
  },
  {
    path: '/product',
    name: 'Product',
    component: () => import('@/views/Product/index.vue'),
  },
  {
    path: '/cart',
    name: 'Cart',
    component: () => import('@/views/Cart/index.vue'),
  },
  {
    path: '/search',
    name: 'Search',
    component: () => import('@/views/Search/index.vue'),
  },
  {
    path: '/store',
    name: 'Store',
    component: () => import('@/views/Store/index.vue'),
  },
  {
    path: '/seckill',
    name: 'Seckill',
    component: () => import('@/views/Seckill/index.vue'),
  },
  {
    path: '/payment',
    name: 'Payment',
    component: () => import('@/views/Payment/index.vue'),
  },
  {
    path: '/mine',
    name: 'Mine',
    redirect: '/mine/my-order',
    component: () => import('@/views/Mine/index.vue'),
    children: [
        {
            path: 'my-order',
            name: 'MyOrder',
            component: () => import('@/views/Mine/components/MyOrder/index.vue'),
        },
        {
            path: 'my-address',
            name: 'MyAddress',
            component: () => import('@/views/Mine/components/MyAddress/index.vue'),
        },
        {
            path: 'my-collect',
            name: 'MyCollect',
            component: () => import('@/views/Mine/components/MyCollect/index.vue')
        },
        {
            path: 'my-coupon',
            name: 'MyCoupon',
            component: () => import('@/views/Mine/components/MyCoupon/index.vue')
        },
        {
            path: 'my-info',
            name: 'MyInfo',
            component: () => import('@/views/Mine/components/MyInfo/index.vue')
        },
        {
            path: 'my-comment',
            name: 'MyComment',
            component: () => import('@/views/Mine/components/MyComment/index.vue')
        },
        {
            path: 'my-cart',
            name: 'MyCart',
            component: () => import('@/views/Mine/components/MyCart/index.vue')
        }
    ],
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  console.log(to.path)
  console.log(to.path !== '/mine/my-order')
  console.log(from.path)
  // 拦截我的下方的所有页面、订单支付页面、购物车页面
  if (to.path !== '/mine/my-order' && to.path !== '/payment' && to.path !== '/mine/my-cart') {
    next()
  }else {
    if (!token) {
      next('/auth')
    } else {
      next()
    }
  }
})

export default router
